package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.CategoryBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-04-28
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements CategoryBrandService {

    @Autowired
    private CategoryBrandMapper categoryBrandMapper;
    @Override
    public List<CategoryBrand> getPageCategoryBrand() {
       List<CategoryBrand> list = categoryBrandMapper.getCategoryBrand();
        return list;
    }

    @Override
    public void saveCategoryBrand(CategoryBrand categoryBrand) {
        LambdaQueryWrapper<CategoryBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CategoryBrand::getCategoryId,categoryBrand.getCategoryId());
        queryWrapper.eq(CategoryBrand::getBrandId,categoryBrand.getBrandId());
        Long categoryBrandCount = baseMapper.selectCount(queryWrapper);
        if(categoryBrandCount>0){
            throw new ServiceException("该分类已有这个品牌");
        }else{
            baseMapper.insert(categoryBrand);
        }
    }

    @Override
    public List<Brand> getcategoryBrandByBrandId(Long brandId) {
        List<Brand> brandList = categoryBrandMapper.getcategoryBrandByBrandId(brandId);
        return brandList;
    }
}
